﻿using System.Collections.Generic;
using Caliburn.Micro;
using SS.WPF.Framework.Localization;

namespace SS.WPF.Framework.Restriction
{
    public class MaxLengthRestriction : Restriction<string>
    {
        public int? MinLength { get; set; }
        public int? MaxLength { get; set; }
        public override bool Validate(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            var gtMin = this.MinLength == null || (value.Length >= this.MinLength);
            var ltMax = this.MaxLength == null || (value.Length <= this.MaxLength);
            return (gtMin && ltMax);
        }

        public override string Description
        {
            get
            {
                var list = new List<string>();
                if (MinLength != null)
                {
                    list.Add(string.Format((string)IoC.Get<TranslationManager>().Translate("Minlength"), MinLength).Trim(new[] { '\n', ' ', '\t' }));
                }

                if (MaxLength != null)
                {
                    list.Add(string.Format((string)IoC.Get<TranslationManager>().Translate("MaxLength"), MaxLength).Trim(new[] { '\n', ' ', '\t' }));
                }

                return string.Join("\t", list.ToArray());
            }
        }

        public override bool CanParse(string text)
        {
            return true;
        }
    }
}